package cn.kxlove.zhbj.fragment;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.kxlove.zhbj.MainActivity;
import cn.kxlove.zhbj.R;
import cn.kxlove.zhbj.pager.BasePager;
import cn.kxlove.zhbj.pager.impl.GovAffairsPager;
import cn.kxlove.zhbj.pager.impl.HomePager;
import cn.kxlove.zhbj.pager.impl.NewsCenterPager;
import cn.kxlove.zhbj.pager.impl.SettingPager;
import cn.kxlove.zhbj.pager.impl.SmartServicePager;

/**
 * @author kxlove 16/1/27 18:40
 */
public class ContentFragment extends BaseFragment {

    @ViewInject(R.id.vp_content)
    private ViewPager mViewPager;

    @ViewInject(R.id.rg_group)
    private RadioGroup rgRroup;

    private List<BasePager> mPagers;

    @Override
    protected View initView() {
        View view = View.inflate(mActivity, R.layout.fragment_content, null);
        x.view().inject(this,view);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mPagers.get(position).initData();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        return view;
    }


    @Event(value = R.id.rg_group,type = RadioGroup.OnCheckedChangeListener.class)
    private void rgRroupChange(RadioGroup group,int checkedId){
        switch (checkedId) {
            case R.id.rb_home:
                // 首页
                // mViewPager.setCurrentItem(0);
                mViewPager.setCurrentItem(0, false);// 禁用页面切换的动画效果
                // 禁用侧边栏
                setSlidingMenuEnable(false);
                break;
            case R.id.rb_news:
                // 新闻中心
                mViewPager.setCurrentItem(1, false);
                // 启用侧边栏
                setSlidingMenuEnable(true);
                break;
            case R.id.rb_smart:
                // 智慧服务
                mViewPager.setCurrentItem(2, false);
                // 启用侧边栏
                setSlidingMenuEnable(true);
                break;
            case R.id.rb_gov:
                // 政务
                mViewPager.setCurrentItem(3, false);
                // 启用侧边栏
                setSlidingMenuEnable(true);
                break;
            case R.id.rb_setting:
                // 设置
                mViewPager.setCurrentItem(4, false);
                // 禁用侧边栏
                setSlidingMenuEnable(false);
                break;

            default:
                break;
        }
    }

    @Override
    protected void initDate() {
        super.initDate();
        // 初始化5个标签页面
        mPagers = new ArrayList<>();
        mPagers.add(new HomePager(mActivity));
        mPagers.add(new NewsCenterPager(mActivity));
        mPagers.add(new SmartServicePager(mActivity));
        mPagers.add(new GovAffairsPager(mActivity));
        mPagers.add(new SettingPager(mActivity));

        mViewPager.setAdapter(new ContentAdapter());

        // 初始化首页数据
        mPagers.get(0).initData();
        // 禁用首页侧边栏
        setSlidingMenuEnable(false);
    }

    private void setSlidingMenuEnable(boolean enable) {
        MainActivity mainUI = (MainActivity) mActivity;
        SlidingMenu slidingMenu = mainUI.getSlidingMenu();
        if (enable) {
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        } else {
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        }
    }

    class ContentAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mPagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePager pager = mPagers.get(position);
            View view = pager.mRootView;// 获取当前页面的根布局
            // pager.initData();// 初始化数据, 不要再此处调用初始化数据方法, 因为此处会默认上一页,当前页和下一页,
            // 浪费流量和性能
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

    }

    /**
     * 获取新闻中心页面
     */
    public NewsCenterPager getNewsCenterPager() {
        NewsCenterPager pager = (NewsCenterPager) mPagers.get(1);
        return pager;
    }

}
